#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#


"""Tests for metadata_utils module."""

import logging
import unittest
import metadata_utils
import sorting_column_sets


class MetadataUtilsTest(unittest.TestCase):

  def setUp(self):
    logging.info('TEST: ' + self.id())
    self.set_manager = sorting_column_sets.SetManager()

  def tearDown(self):
    self.set_manager.Clear()

  def testGenerateLevelMap(self):
    """Tests the method GenerateLevelMap."""
    level_map = metadata_utils.GenerateLevelMap([], 1)
    self.assertEqual(0, len(level_map))

    level_map = metadata_utils.GenerateLevelMap(['apple', 'bat', 'cat'], 1)
    self.assertEqual(len(level_map), 3)
    self.assertEqual(level_map[0]['name'], 'level1_1')
    self.assertEqual(level_map[0]['value'], 'apple')
    self.assertEqual(level_map[1]['name'], 'level1_2')
    self.assertEqual(level_map[1]['value'], 'bat')
    self.assertEqual(level_map[2]['name'], 'level1_3')
    self.assertEqual(level_map[2]['value'], 'cat')

  def testGetUniqueValueMaps(self):
    """Tests the method GetUniqueValueMaps."""
    self.set_manager.UpdateColumnSets('row1', 'fruits', 'apple', 'yellow')
    self.set_manager.UpdateColumnSets('row2', 'fruits', 'banana', 'yellow')
    self.set_manager.UpdateColumnSets('row3', 'animals', 'cat', 'black')
    self.set_manager.UpdateColumnSets('row4', 'animals', 'dog', 'black')

    value_maps = metadata_utils.GetUniqueValueMaps()
    self.assertEqual(len(value_maps), 3)
    self.assertEqual(len(value_maps[0]), 2)
    self.assertEqual(value_maps[0][0]['name'], 'level1_1')
    self.assertEqual(value_maps[0][0]['value'], 'fruits')
    self.assertEqual(value_maps[0][1]['name'], 'level1_2')
    self.assertEqual(value_maps[0][1]['value'], 'animals')
    self.assertEqual(len(value_maps[1]), 4)
    self.assertEqual(value_maps[1][0]['name'], 'level2_1')
    self.assertEqual(value_maps[1][0]['value'], 'apple')
    self.assertEqual(value_maps[1][1]['name'], 'level2_2')
    self.assertEqual(value_maps[1][1]['value'], 'banana')
    self.assertEqual(value_maps[1][2]['name'], 'level2_3')
    self.assertEqual(value_maps[1][2]['value'], 'cat')
    self.assertEqual(value_maps[1][3]['name'], 'level2_4')
    self.assertEqual(value_maps[1][3]['value'], 'dog')
    self.assertEqual(len(value_maps[2]), 2)
    self.assertEqual(value_maps[2][0]['name'], 'level3_1')
    self.assertEqual(value_maps[2][0]['value'], 'yellow')
    self.assertEqual(value_maps[2][1]['name'], 'level3_2')
    self.assertEqual(value_maps[2][1]['value'], 'black')

if __name__ == '__main__':
  unittest.main()
